var CONTEXT_PATH = "/answer";

window.alert = function(message) {
	if(!$(".alert-box").length) {
		$("body").append(
			'<div class="modal alert-box" tabindex="-1" role="dialog">'+
				'<div class="modal-dialog" role="document">'+
				'<div class="modal-content">'+
					'<div class="modal-header">'+
						'<h5 class="modal-title">提示</h5>'+
						'<button type="button" class="close" data-dismiss="modal" aria-label="Close">'+
							'<span aria-hidden="true">&times;</span>'+
						'</button>'+
					'</div>'+
					'<div class="modal-body">'+
						'<p></p>'+
					'</div>'+
					'<div class="modal-footer">'+
						'<button type="button" class="btn btn-secondary" data-dismiss="modal">确定</button>'+
					'</div>'+
					'</div>'+
				'</div>'+
			'</div>'
		);
	}

    var h = $(".alert-box").height();
	var y = h / 2 - 100;
	if(h > 600) y -= 100;
    $(".alert-box .modal-dialog").css("margin", (y < 0 ? 0 : y) + "px auto");
	
	$(".alert-box .modal-body p").text(message);
	$(".alert-box").modal("show");
}

var IP = "http://127.0.0.1:9200/";

function mesg(content) {
	layui.use('layer', function(){
		layer.open({
			title: '提示'
			,offset: '250px'
			,content: content
			,yes: function(){
				window.location.href = "register.html";
			}
		});
	});
}

function sendPost(url,data){
	let result = null;
	$.ajax({
		url: IP+url,
		cache: false,
		async: false,
		data: JSON.stringify(data),
		type: "Post",
		contentType: 'application/json',
		dataType: "json",
		success: function (data) {
			result = data
		}
	})
	return result;
}
function jsonPost(url,data){
	let result = null;
	$.ajax({
		url: IP+url,
		cache: false,
		async: false,
		data: data,
		type: "Post",
		contentType: 'application/x-www-form-urlencoded',
		dataType: "json",
		success: function (data) {
			result = data
		}
	})
	return result;
}
function sendPut(url,data){
	let result = null;
	$.ajax({
		url: IP+url,
		cache: false,
		async: false,
		data: data,
		type: "PUT",
		contentType: 'application/json; charset=UTF-8',
		dataType: "json",
		success: function (data) {
			result = data
		}
	})
	return result;
}

function sendGet(url,data){
	let result = null;
	$.ajax({
		url: IP+url,
		cache: false,
		async: false,
		data: data,
		type: "Get",
		contentType: 'application/json; charset=UTF-8',
		dataType: "json",
		beforeSend: function(request) {
			var localData = layui.data('LocalData');
			request.setRequestHeader("authorization", localData["access_token_front"]);
		},
		success: function (data) {
			result = data
		}
	})
	return result;
}